import * as PIXI from 'pixi.js';
import { VehicleConfig } from '../config';

export type VehicleType = 'car' | 'bus' | 'truck' | 'van';

export class Vehicle extends PIXI.Container {
    private graphics: PIXI.Graphics;
    private config: VehicleConfig;
    private lane: number;
    public isActive: boolean = true;
    private speedMultiplier: number = 1;

    constructor(type: VehicleType, config: VehicleConfig, laneIndex: number, laneWidth: number, speedMultiplier: number = 1) {
        super();
        this.config = config;
        this.lane = laneIndex;
        this.speedMultiplier = speedMultiplier;
        
        // 创建车辆图形
        this.graphics = this.createVehicleGraphics(type);
        this.addChild(this.graphics);
        
        // 设置初始位置（车道中间）
        const laneCenter = (laneIndex * laneWidth) + (laneWidth / 2);
        this.x = laneCenter;
        this.y = -this.config.length;  // 从屏幕上方开始
    }

    private getRandomCarColor(): number {
        const colors = [
            0xe74c3c,  // 红色
            0x3498db,  // 蓝色
            0x2ecc71,  // 绿色
            0xf1c40f,  // 黄色
            0x95a5a6,  // 银色
            0x34495e,  // 深蓝色
            0xecf0f1,  // 白色
            0x2c3e50   // 黑色
        ];
        return colors[Math.floor(Math.random() * colors.length)];
    }

    private createVehicleGraphics(type: VehicleType): PIXI.Graphics {
        const graphics = new PIXI.Graphics();
        const width = this.config.width;
        const length = this.config.length;
        
        // 根据车型选择基本颜色
        let mainColor: number;
        const wheelColor = 0x2c3e50;
        const windowColor = 0x2c3e50;
        const lightColor = 0xf1c40f;
        const highlightColor = 0xffffff;
        
        if (this.speedMultiplier > 1) {
            mainColor = 0xe74c3c;  // 超速车辆使用红色
        } else {
            switch (type) {
                case 'car':
                    mainColor = this.getRandomCarColor();
                    break;
                case 'bus':
                    mainColor = 0xf1c40f;  // 黄色
                    break;
                case 'truck':
                    mainColor = 0x27ae60;  // 绿色
                    break;
                case 'van':
                    mainColor = 0x9b59b6;  // 紫色
                    break;
            }
        }

        switch (type) {
            case 'car':
                this.drawModernCar(graphics, width, length, mainColor, wheelColor, windowColor, lightColor, highlightColor);
                break;
            case 'bus':
                this.drawModernBus(graphics, width, length, mainColor, wheelColor, windowColor, lightColor, highlightColor);
                break;
            case 'truck':
                this.drawModernTruck(graphics, width, length, mainColor, wheelColor, windowColor, lightColor, highlightColor);
                break;
            case 'van':
                this.drawModernVan(graphics, width, length, mainColor, wheelColor, windowColor, lightColor, highlightColor);
                break;
        }
        
        return graphics;
    }

    private drawWheel(graphics: PIXI.Graphics, x: number, y: number, size: number, color: number): void {
        graphics.lineStyle(1, 0x2c3e50);
        graphics.beginFill(color);
        graphics.drawCircle(x, y, size);
        graphics.endFill();
        // 轮毂
        graphics.beginFill(0x95a5a6);
        graphics.drawCircle(x, y, size * 0.4);
        graphics.endFill();
    }

    private drawModernCar(graphics: PIXI.Graphics, width: number, length: number, mainColor: number, 
                         wheelColor: number, windowColor: number, lightColor: number, highlightColor: number): void {
        const w = width / 2;
        const l = length / 2;
        
        // 车身主体
        graphics.lineStyle(1, 0x2c3e50);
        graphics.beginFill(mainColor);
        
        // 车身轮廓（圆角矩形）
        const cornerRadius = 8;
        graphics.drawRoundedRect(-w, -length, width, length, cornerRadius);
        
        // 车顶（弧形）
        graphics.beginFill(mainColor);
        graphics.moveTo(-w * 0.8, -length * 0.7);
        graphics.lineTo(w * 0.8, -length * 0.7);
        graphics.quadraticCurveTo(w * 0.8, -length * 0.85, 0, -length * 0.85);
        graphics.quadraticCurveTo(-w * 0.8, -length * 0.85, -w * 0.8, -length * 0.7);
        graphics.endFill();

        // 车窗
        graphics.beginFill(windowColor);
        // 前挡风
        graphics.moveTo(-w * 0.7, -length * 0.7);
        graphics.lineTo(w * 0.7, -length * 0.7);
        graphics.lineTo(w * 0.5, -length * 0.85);
        graphics.lineTo(-w * 0.5, -length * 0.85);
        graphics.lineTo(-w * 0.7, -length * 0.7);
        // 后挡风
        graphics.moveTo(-w * 0.7, -length * 0.4);
        graphics.lineTo(w * 0.7, -length * 0.4);
        graphics.lineTo(w * 0.5, -length * 0.25);
        graphics.lineTo(-w * 0.5, -length * 0.25);
        graphics.lineTo(-w * 0.7, -length * 0.4);
        graphics.endFill();

        // 车灯
        // 前灯
        graphics.beginFill(lightColor);
        graphics.drawEllipse(-w * 0.7, -length * 0.1, w * 0.2, w * 0.15);
        graphics.drawEllipse(w * 0.7, -length * 0.1, w * 0.2, w * 0.15);
        // 后灯
        graphics.beginFill(0xe74c3c);
        graphics.drawEllipse(-w * 0.7, -length * 0.9, w * 0.15, w * 0.1);
        graphics.drawEllipse(w * 0.7, -length * 0.9, w * 0.15, w * 0.1);
        graphics.endFill();

        // 轮子
        this.drawWheel(graphics, -w * 0.8, -length * 0.2, w * 0.25, wheelColor);
        this.drawWheel(graphics, w * 0.8, -length * 0.2, w * 0.25, wheelColor);
        this.drawWheel(graphics, -w * 0.8, -length * 0.8, w * 0.25, wheelColor);
        this.drawWheel(graphics, w * 0.8, -length * 0.8, w * 0.25, wheelColor);

        // 装饰线条
        graphics.lineStyle(2, highlightColor, 0.3);
        graphics.moveTo(-w * 0.5, -length * 0.55);
        graphics.lineTo(w * 0.5, -length * 0.55);
    }

    private drawModernBus(graphics: PIXI.Graphics, width: number, length: number, mainColor: number, 
                         wheelColor: number, windowColor: number, lightColor: number, highlightColor: number): void {
        const w = width / 2;
        const l = length / 2;
        
        // 主体
        graphics.lineStyle(1, 0x2c3e50);
        graphics.beginFill(mainColor);
        graphics.drawRoundedRect(-w, -length, width, length, 10);
        graphics.endFill();

        // 窗户
        graphics.beginFill(windowColor);
        // 驾驶室窗户
        graphics.drawRect(-w * 0.9, -length * 0.9, width * 0.4, length * 0.2);
        // 乘客窗户
        for (let i = 0; i < 4; i++) {
            graphics.drawRoundedRect(-w * 0.9, -length * (0.65 - i * 0.15), 
                                   width * 0.8, length * 0.1, 5);
        }
        graphics.endFill();

        // 车灯
        graphics.beginFill(lightColor);
        graphics.drawRect(-w * 0.9, -length * 0.05, w * 0.3, w * 0.2);
        graphics.drawRect(w * 0.6, -length * 0.05, w * 0.3, w * 0.2);
        graphics.endFill();

        // 后灯
        graphics.beginFill(0xe74c3c);
        graphics.drawRect(-w * 0.9, -length * 0.98, w * 0.3, w * 0.2);
        graphics.drawRect(w * 0.6, -length * 0.98, w * 0.3, w * 0.2);
        graphics.endFill();

        // 轮子
        this.drawWheel(graphics, -w * 0.6, -length * 0.15, w * 0.3, wheelColor);
        this.drawWheel(graphics, w * 0.6, -length * 0.15, w * 0.3, wheelColor);
        this.drawWheel(graphics, -w * 0.6, -length * 0.85, w * 0.3, wheelColor);
        this.drawWheel(graphics, w * 0.6, -length * 0.85, w * 0.3, wheelColor);

        // 装饰条
        graphics.lineStyle(3, highlightColor, 0.3);
        graphics.moveTo(-w, -length * 0.5);
        graphics.lineTo(w, -length * 0.5);
    }

    private drawModernTruck(graphics: PIXI.Graphics, width: number, length: number, mainColor: number, 
                           wheelColor: number, windowColor: number, lightColor: number, highlightColor: number): void {
        const w = width / 2;
        const l = length / 2;
        
        // 驾驶室（前部）
        graphics.lineStyle(1, 0x2c3e50);
        graphics.beginFill(mainColor);
        graphics.drawRoundedRect(-w * 0.9, -length * 0.35, width * 0.9, length * 0.35, 8);
        
        // 货箱（后部）
        graphics.beginFill(mainColor);
        graphics.drawRoundedRect(-w, -length, width * 1.0, length * 0.7, 5);
        graphics.endFill();

        // 驾驶室窗户
        graphics.beginFill(windowColor);
        graphics.drawRoundedRect(-w * 0.7, -length * 0.32, width * 0.5, length * 0.15, 5);
        graphics.endFill();

        // 前车灯
        graphics.beginFill(lightColor);
        graphics.drawEllipse(-w * 0.6, -length * 0.08, w * 0.15, w * 0.1);
        graphics.drawEllipse(w * 0.6, -length * 0.08, w * 0.15, w * 0.1);
        graphics.endFill();

        // 后车灯
        graphics.beginFill(0xe74c3c);
        graphics.drawRect(-w * 0.8, -length * 0.98, w * 0.2, w * 0.1);
        graphics.drawRect(w * 0.6, -length * 0.98, w * 0.2, w * 0.1);
        graphics.endFill();

        // 轮子
        // 前轮
        this.drawWheel(graphics, -w * 0.6, -length * 0.15, w * 0.2, wheelColor);
        this.drawWheel(graphics, w * 0.6, -length * 0.15, w * 0.2, wheelColor);
        // 后轮组（双轮）
        this.drawWheel(graphics, -w * 0.8, -length * 0.8, w * 0.2, wheelColor);
        this.drawWheel(graphics, -w * 0.4, -length * 0.8, w * 0.2, wheelColor);
        this.drawWheel(graphics, w * 0.4, -length * 0.8, w * 0.2, wheelColor);
        this.drawWheel(graphics, w * 0.8, -length * 0.8, w * 0.2, wheelColor);

        // 装饰线条
        graphics.lineStyle(2, highlightColor, 0.3);
        // 侧面装饰线
        graphics.moveTo(-w * 0.9, -length * 0.6);
        graphics.lineTo(w * 0.9, -length * 0.6);
        // 货箱轮廓加强
        graphics.lineStyle(2, 0x2c3e50, 0.5);
        graphics.moveTo(-w, -length);
        graphics.lineTo(-w, -length * 0.3);
        graphics.moveTo(w, -length);
        graphics.lineTo(w, -length * 0.3);
    }

    private drawModernVan(graphics: PIXI.Graphics, width: number, length: number, mainColor: number, 
                         wheelColor: number, windowColor: number, lightColor: number, highlightColor: number): void {
        const w = width / 2;
        const l = length / 2;
        
        // 主体
        graphics.lineStyle(1, 0x2c3e50);
        graphics.beginFill(mainColor);
        
        // 车身（圆角矩形）
        graphics.drawRoundedRect(-w, -length, width, length, 10);
        
        // 前部斜面
        graphics.moveTo(-w, -length * 0.7);
        graphics.lineTo(w, -length * 0.7);
        graphics.lineTo(w, -length * 0.9);
        graphics.lineTo(-w, -length * 0.9);
        graphics.lineTo(-w, -length * 0.7);
        graphics.endFill();

        // 窗户
        graphics.beginFill(windowColor);
        // 前窗
        graphics.drawRoundedRect(-w * 0.8, -length * 0.85, width * 0.6, length * 0.2, 5);
        // 侧窗
        graphics.drawRoundedRect(-w * 0.8, -length * 0.6, width * 0.3, length * 0.2, 5);
        graphics.drawRoundedRect(w * 0.5, -length * 0.6, width * 0.3, length * 0.2, 5);
        graphics.endFill();

        // 车灯
        graphics.beginFill(lightColor);
        graphics.drawEllipse(-w * 0.7, -length * 0.1, w * 0.2, w * 0.15);
        graphics.drawEllipse(w * 0.7, -length * 0.1, w * 0.2, w * 0.15);
        graphics.endFill();

        // 后灯
        graphics.beginFill(0xe74c3c);
        graphics.drawRect(-w * 0.8, -length * 0.98, w * 0.3, w * 0.15);
        graphics.drawRect(w * 0.5, -length * 0.98, w * 0.3, w * 0.15);
        graphics.endFill();

        // 轮子
        this.drawWheel(graphics, -w * 0.7, -length * 0.2, w * 0.25, wheelColor);
        this.drawWheel(graphics, w * 0.7, -length * 0.2, w * 0.25, wheelColor);
        this.drawWheel(graphics, -w * 0.7, -length * 0.8, w * 0.25, wheelColor);
        this.drawWheel(graphics, w * 0.7, -length * 0.8, w * 0.25, wheelColor);

        // 装饰条
        graphics.lineStyle(2, highlightColor, 0.3);
        graphics.moveTo(-w, -length * 0.4);
        graphics.lineTo(w, -length * 0.4);
    }

    public update(speed: number, delta: number): void {
        if (!this.isActive) return;
        
        // 使用速度倍率更新位置
        this.y += speed * this.speedMultiplier * delta / 60;
    }

    public getBounds(): PIXI.Rectangle {
        return new PIXI.Rectangle(
            this.x - this.config.width / 2,
            this.y - this.config.length,
            this.config.width,
            this.config.length
        );
    }

    public getLane(): number {
        return this.lane;
    }

    public getSpeedMultiplier(): number {
        return this.speedMultiplier;
    }

    public deactivate(): void {
        this.isActive = false;
    }
}